package com.swiot.biz.controller;

import com.swiot.biz.domain.AlertRecord;
import com.swiot.biz.domain.Remind;
import com.swiot.biz.service.IAlertRecordService;
import com.swiot.biz.service.IRemindService;
import com.swiot.common.annotation.Log;
import com.swiot.common.core.controller.BaseController;
import com.swiot.common.core.domain.AjaxResult;
import com.swiot.common.core.page.TableDataInfo;
import com.swiot.common.enums.BusinessType;
import com.swiot.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 告警提醒Controller
 *
 * @author kingdom
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/biz/remind")
public class RemindController extends BaseController {
    @Autowired
    private IRemindService remindService;

    @Autowired
    private IAlertRecordService alertRecordService;

    /**
     * 查询告警提醒列表
     */
    @PreAuthorize("@ss.hasPermi('biz:remind:list')")
    @GetMapping("/list")
    public TableDataInfo list(Remind remind) {
        startPage();
        List<Remind> list = remindService.selectRemindList(remind);
        return getDataTable(list);
    }

    /**
     * 导出告警提醒列表
     */
    @PreAuthorize("@ss.hasPermi('biz:remind:export')")
    @Log(title = "告警提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Remind remind) {
        List<Remind> list = remindService.selectRemindList(remind);
        ExcelUtil<Remind> util = new ExcelUtil<Remind>(Remind.class);
        util.exportExcel(response, list, "告警提醒数据");
    }

    /**
     * 获取告警提醒详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:remind:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(remindService.selectRemindById(id));
    }

    /**
     * 获取告警提醒详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:remind:query')")
    @GetMapping(value = "/alert/{id}")
    public AjaxResult getAlertInfo(@PathVariable("id") Long id) {
        return success(remindService.selectRemindByAlertId(id));
    }


    /**
     * 新增告警提醒
     */
    @PreAuthorize("@ss.hasPermi('biz:remind:add')")
    @Log(title = "告警提醒", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Remind remind) {
        if (remind.getTemplateDataId() == null) {
            return AjaxResult.error("模板数据ID不能为空");
        }
        if (remind.getAlarmId() == null) {
            return AjaxResult.error("告警ID不能为空");
        }
        // 检查告警记录是否存在
        AlertRecord alertRecord = alertRecordService.selectAlertRecordById(remind.getAlarmId().toString());
        if (alertRecord == null) {
            return AjaxResult.error("告警记录不存在");
        }

        remind.setReportDate(alertRecord.getReportDate());
        remind.setDeptId(alertRecord.getDeptId());
        remind.setSegmentName(alertRecord.getSegmentName());

        return toAjax(remindService.insertRemind(remind));
    }

    /**
     * 修改告警提醒
     */
    @PreAuthorize("@ss.hasPermi('biz:remind:edit')")
    @Log(title = "告警提醒", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Remind remind) {
        return toAjax(remindService.updateRemind(remind));
    }

    /**
     * 删除告警提醒
     */
    @PreAuthorize("@ss.hasPermi('biz:remind:remove')")
    @Log(title = "告警提醒", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(remindService.deleteRemindByIds(ids));
    }
}
